package com.yougou.ui.cart.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yougou.R;
import com.yougou.data.model.CartItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
    
    private List<CartItem> mData = new ArrayList<>();
    private OnCartItemChangedListener mListener;
    
    public interface OnCartItemChangedListener {
        void onCheckedChanged(CartItem item, boolean checked);
        void onQuantityChanged(CartItem item, int quantity);
        void onItemClick(CartItem item);
    }
    
    public void setOnCartItemChangedListener(OnCartItemChangedListener listener) {
        this.mListener = listener;
    }
    
    public void setData(List<CartItem> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    
    public List<CartItem> getData() {
        return mData;
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_cart, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CartItem item = mData.get(position);
        
        // 设置选中状态
        holder.cbSelect.setOnCheckedChangeListener(null);
        holder.cbSelect.setChecked(item.isChecked());
        holder.cbSelect.setOnCheckedChangeListener((buttonView, isChecked) -> {
            item.setChecked(isChecked);
            if (mListener != null) {
                mListener.onCheckedChanged(item, isChecked);
            }
        });
        
        // 商品图片
        String imageUrl = item.getGoods().getGoodsPic();
        if (imageUrl != null && !imageUrl.trim().isEmpty()) {
            Glide.with(holder.itemView.getContext())
                    .load(imageUrl)
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.placeholder_image)
                    .into(holder.ivGoodsImage);
        } else {
            holder.ivGoodsImage.setImageResource(R.drawable.placeholder_image);
        }
        
        // 商品名称
        holder.tvGoodsName.setText(item.getGoods().getGoodsName());
        
        // 价格
        holder.tvPrice.setText(String.format(Locale.CHINA, "¥%.2f", item.getGoods().getGoodsPrice()));
        
        // 数量
        holder.tvQuantity.setText(String.valueOf(item.getQuantity()));
        
        // 减少按钮
        holder.tvDecrease.setOnClickListener(v -> {
            int newQuantity = item.getQuantity() - 1;
            if (newQuantity >= 1) {
                item.setQuantity(newQuantity);
                holder.tvQuantity.setText(String.valueOf(newQuantity));
                if (mListener != null) {
                    mListener.onQuantityChanged(item, newQuantity);
                }
            }
        });
        
        // 增加按钮
        holder.tvIncrease.setOnClickListener(v -> {
            int newQuantity = item.getQuantity() + 1;
            item.setQuantity(newQuantity);
            holder.tvQuantity.setText(String.valueOf(newQuantity));
            if (mListener != null) {
                mListener.onQuantityChanged(item, newQuantity);
            }
        });
        
        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemClick(item);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return mData.size();
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox cbSelect;
        ImageView ivGoodsImage;
        TextView tvGoodsName;
        TextView tvPrice;
        TextView tvQuantity;
        TextView tvDecrease;
        TextView tvIncrease;
        
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            cbSelect = itemView.findViewById(R.id.cb_select);
            ivGoodsImage = itemView.findViewById(R.id.iv_goods_image);
            tvGoodsName = itemView.findViewById(R.id.tv_goods_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvQuantity = itemView.findViewById(R.id.tv_quantity);
            tvDecrease = itemView.findViewById(R.id.tv_decrease);
            tvIncrease = itemView.findViewById(R.id.tv_increase);
        }
    }
}

